<?php

/**
 * users actions.
 *
 * @package    rms
 * @subpackage users
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class usersActions extends sfActions
{
  public function executeLogin(sfWebRequest $request)
  {
    $this->form = new LoginForm();
 
    if($request->isMethod("post"))
    {
      $this->form->bind( $request->getParameter("login"));

      if( $this->form->isValid())
      {
        if( !$user = UsersTable::login( $this->form->getValue("login"), $this->form->getValue("password")))
        {
          $this->getUser()->setFlash("error", "datos incorrectos");
          $this->redirect("@user_login");
        }
        else
        {
          $this->getUser()->setAuthenticated( true );
          $this->getUser()->setAttribute( "id", $user->id );
          $this->getUser()->setAttribute( "first_name", $user->first_name );
          $this->getUser()->setAttribute( "last_name", $user->last_name );

          // Comprobamos si tiene referer, si no, le llevamos a la homepage
          $url = $this->getUser()->getAttribute("referer",false)?:"@homepage";
          $this->getUser()->setAttribute("referer", false);
          $this->redirect($url);
        }
      }
    }     
  }

  public function executeLogout(sfWebRequest $request)
  {
    $this->getUser()->setAuthenticated( false );
    $this->getUser()->getAttributeHolder()->clear();
    $this->redirect("@homepage");
  }

  public function executeRegister(sfWebRequest $request)
  {
    $this->form = new UsersForm();
// 
    if($request->isMethod("post"))
    {
      $this->form->bind( $request->getParameter( $this->form->getName()));
      if( $this->form->isValid())
      {
           $user = new Users();
           $user->role = $this->form->getValue("Rol");
           $user->username = $this->form->getValue("Username");
           $user->password = md5( $this->form->getValue("Contraseña"));
           $user->first_name = $this->form->getValue("Nombre(s)");
           $user->last_name = $this->form->getValue("Apellido(s)");
           $user->email = $this->form->getValue("E-Mail");
           $user->approved = $this->form->getValue("Activo");
           $user->save();
 
           $this->redirect('@homepage');
      }
    }
  }

  public function executeIndex(sfWebRequest $request)
  {
    $this->userss = Doctrine_Core::getTable('Users')->getUsers();
  }

  public function executeShow(sfWebRequest $request)
  {
    $this->users = Doctrine_Core::getTable('Users')->find(array($request->getParameter('id')));
    $this->forward404Unless($this->users);
  }

//   public function executeNew(sfWebRequest $request)
//   {
//     $this->form = new UsersForm();
//   }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));

    $this->form = new UsersForm();

    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($users = Doctrine_Core::getTable('Users')->find(array($request->getParameter('id'))), sprintf('Object users does not exist (%s).', $request->getParameter('id')));
    $this->form = new UsersForm($users);
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $this->forward404Unless($users = Doctrine_Core::getTable('Users')->find(array($request->getParameter('id'))), sprintf('Object users does not exist (%s).', $request->getParameter('id')));
    $this->form = new UsersForm($users);

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->forward404Unless($users = Doctrine_Core::getTable('Users')->find(array($request->getParameter('id'))), sprintf('Object users does not exist (%s).', $request->getParameter('id')));
    $users->delete();

    $this->redirect('users/index');
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $users = $form->save();

      $this->redirect('users/edit?id='.$users->getId());
    }
  }
}
